class TourGroup < ActiveRecord::Base
	belongs_to :nationality
	belongs_to :customer
	belongs_to :owner, :class_name => "Employee", :foreign_key => "owner_id"
	has_one :lodging, :dependent => :destroy
	has_one :guide_involved, :dependent => :destroy
  has_one :vehicle, :dependent => :destroy
  has_one :shopping, :dependent => :destroy
  has_one :outer_expense, :dependent => :destroy
  
  validates_presence_of :number, :message => "请输入团号"
  validates_uniqueness_of :number, :message => "已经存在同名团号"
  validates_presence_of :sequence, :message => "请输入序号"
  
  def TourGroup.next_sequence()
    last_year = DateTime.now.last_year.year
    from = DateTime.new(last_year, 12, 16)
    to = DateTime.new(DateTime.now.year, 12, 15, 23, 59, 59)
    seq = TourGroup.maximum("sequence", {:conditions => ["during_start between ? and ?", from, to]})
    seq.nil? ? 1 : seq + 1
  end
  
#  def load_from_file_field(file_field)
#    #self.name = file_field.original_filename
#    #self.content_type = file_field.content_type.chomp
#    data = file_field.read
#    objs = YAML.load_stream(data)
#    puts objs
#  end
end
